<?php
App::uses('AppController', 'Controller');
/**
 * Surveys Controller
 *
 * @property Survey $Survey
 */
class SurveysController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Survey->recursive = 0;

		$this->paginate = array(
	        'conditions' => array('Survey.user_id' => $this->Auth->user('id')),
	        'order' => array('Survey.created ASC'),
	    );

		$this->set('surveys', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Survey->exists($id)) {
			throw new NotFoundException(__('Invalid survey'));
		}
		$survey = $this->Survey->findById($id);
		$survey['Question'] = $this->Survey->Question->find('all', array(
			'conditions' => array(
				'Question.survey_id' => $id,
			),
			'order' => array(
				'Question.numeral ASC',
			),
		));
		$this->set(compact('survey'));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post') || $this->request->is('put')) {
			$this->Survey->create();
			$this->Survey->set('user_id', $this->Auth->user('id'));
			$this->Survey->set('identifier', uniqid());

			if ($this->Survey->save($this->request->data)) {
				$this->Flash->success("Survey created successfully!");
				$this->redirect(array('controller' => 'Questions', 'action' => 'index', $this->Survey->id));
			} else {
				$this->Flash->error("The Survey could not be created.");
			}
		}
		$users = $this->Survey->User->find('list');
		$this->set(compact('users'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Survey->exists($id)) {
			throw new NotFoundException(__('Invalid survey'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			$theSurvey = $this->Survey->findById($id);
			foreach ($this->request->data['Survey'] as $field => $newVal) {
				$theSurvey['Survey'][$field] = $newVal;
			}
			if ($this->Survey->save($theSurvey)) {
				$this->Flash->success("Survey saved successfully!");
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Flash->error("Error saving Survey data!");
			}
		} else {
			$this->request->data = $this->Survey->findById($id);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Survey->id = $id;
		if (!$this->Survey->exists()) {
			throw new NotFoundException(__('Invalid survey'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Survey->delete()) {
			$this->Flash->success("Survey deleted successfully!");
			$this->redirect(array('action' => 'index'));
		}
		$this->Flash->error("There was a problem deleting the Survey.");
		$this->redirect(array('action' => 'index'));
	}

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->Survey->recursive = 0;
		$this->set('surveys', $this->paginate());
	}

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		if (!$this->Survey->exists($id)) {
			throw new NotFoundException(__('Invalid survey'));
		}
		$options = array('conditions' => array('Survey.' . $this->Survey->primaryKey => $id));
		$this->set('survey', $this->Survey->find('first', $options));
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post') || $this->request->is('put')) {
			$this->Survey->create();
			if ($this->Survey->save($this->request->data)) {
				$this->Session->setFlash(__('The survey has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The survey could not be saved. Please, try again.'));
			}
		}
		$users = $this->Survey->User->find('list');
		$this->set(compact('users'));
	}

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->Survey->exists($id)) {
			throw new NotFoundException(__('Invalid survey'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Survey->save($this->request->data)) {
				$this->Session->setFlash(__('The survey has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The survey could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Survey.' . $this->Survey->primaryKey => $id));
			$this->request->data = $this->Survey->find('first', $options);
		}
		$users = $this->Survey->User->find('list');
		$this->set(compact('users'));
	}

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->Survey->id = $id;
		if (!$this->Survey->exists()) {
			throw new NotFoundException(__('Invalid survey'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Survey->delete()) {
			$this->Session->setFlash(__('Survey deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Survey was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
